package com.kyrie.lib_sys.common.fileApplys.service;

import com.kyrie.lib_sys.common.fileApplys.util.MinioUtil;
import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件--service层
 *
 * @author heefM
 * @date 2025-09-01
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService{
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * 文件上传
     */
    @Override
    public String uploadFile(MultipartFile uploadFile, String bucketName, String objectName) {
        try {
            // 如果没有指定objectName，则使用原始文件名
            if (objectName == null || objectName.isEmpty()) {
                objectName = uploadFile.getOriginalFilename();
            }

            // 调用MinioUtil上传文件
            minioUtil.uploadFile(uploadFile.getInputStream(), bucketName, objectName);

            // 返回文件的预览URL
            return minioUtil.getPreviewFileUrl(bucketName, objectName);
        } catch (Exception e) {
            log.error("文件上传失败: ", e);
            throw new RuntimeException("文件上传失败: " + e.getMessage());
        }
    }
}
